package com.zhengtoudai.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.BaseActivity;
import com.zhengtoudai.global.Config;
import com.zhengtoudai.http.HttpUtil;
import com.zhengtoudai.injector.component.DaggerActivityComponent;
import com.zhengtoudai.ui.bean.Result;
import com.zhengtoudai.ui.presenter.ForgetPasswordPresenterImpl;
import com.zhengtoudai.ui.presenter.VerificationCodePresenterImpl;
import com.zhengtoudai.ui.view.CommonView;
import com.zhengtoudai.ui.view.VerificationCodeView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.util.Tool;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPasswordActivity extends BaseActivity implements CommonView, VerificationCodeView {

    @Bind(R.id.edt_mobile_phone)
    EditText mEdtMobilePhone;
    @Bind(R.id.edt_verification_code)
    EditText mEdtVerificationCode;
    @Bind(R.id.tv_code)
    TextView mTvCode;
    @Bind(R.id.edt_password)
    EditText mEdtPassword;
    @Bind(R.id.edt_affirm_password)
    EditText mEdtAffirmPassword;

    @Inject
    ForgetPasswordPresenterImpl mPresenter;

    @Inject
    VerificationCodePresenterImpl mSMSPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        ButterKnife.bind(this);
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitle("忘记密码", false);
    }

    @Override
    protected void initView() {
        DaggerActivityComponent.builder().appComponent(App.mApp.getAppComponent())
                .activityModule(getActivityModule())
                .build()
                .inject(this);
        mPresenter.attachView(this);
        mSMSPresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
        mSMSPresenter.detachView();
    }


    @OnClick({R.id.tv_code, R.id.btn_find})
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        mEdtMobilePhone.setError(null);
        mEdtVerificationCode.setError(null);
        mEdtPassword.setError(null);
        mEdtAffirmPassword.setError(null);
        String mobilePhone = mEdtMobilePhone.getText().toString().trim();
        if (TextUtils.isEmpty(mobilePhone) || mobilePhone.length() != 11) {
            mEdtMobilePhone.setError("请输入11位手机号码");
            //AbToastUtil.showToast(context, "请输入11位手机号码");
            mEdtMobilePhone.requestFocus();
            return;
        }

        switch (view.getId()) {
            case R.id.tv_code:
                mSMSPresenter.getSMSCode(mobilePhone);
                break;
            case R.id.btn_find:
                String mCode = mEdtVerificationCode.getText().toString().trim();
                String mPassword = mEdtPassword.getText().toString().trim();
                String mAffirmPassword = mEdtAffirmPassword.getText().toString().trim();
                if (TextUtils.isEmpty(mCode)) {
                    mEdtVerificationCode.setError("请输入验证码");
                    mEdtVerificationCode.requestFocus();
                } else if (TextUtils.isEmpty(mPassword)) {
                    mEdtPassword.setError("请输入密码");
                    //AbToastUtil.showToast(context, "请输入密码");
                    mEdtPassword.requestFocus();
                } else if (TextUtils.isEmpty(mAffirmPassword)) {
                    mEdtAffirmPassword.setError("请再次输入密码");
                    //AbToastUtil.showToast(context, "请再次输入密码");
                    mEdtAffirmPassword.requestFocus();
                } else if (mPassword.length() < 6) {
                    mEdtPassword.setError("密码不得小于6位数");
                    //AbToastUtil.showToast(context, "密码不得小于6位数");
                    mEdtPassword.requestFocus();
                } else if (mAffirmPassword.length() < 6) {
                    mEdtAffirmPassword.setError("密码不得小于6位数");
                    //AbToastUtil.showToast(context, "密码不得小于6位数");
                    mEdtAffirmPassword.requestFocus();
                } else if (!mPassword.equals(mAffirmPassword)) {
                    mEdtAffirmPassword.setError("两次密码输入不一致");
                    //AbToastUtil.showToast(context, "两次密码输入不一致");
                    mEdtAffirmPassword.requestFocus();
                } else {
                    mPresenter.forgetPassword(mobilePhone, mCode, mPassword, mAffirmPassword);
                }

                break;
        }
    }


    @Override
    public void showSMSDialog() {
        showProgressDialog("正在获取验证码...");
    }

    @Override
    public void onSMSResult(Result result) {
        if (result.getCode() == Config.SUCCESSFUL_CODE) {
            Tool.countTime(60, mTvCode);
        }
        AbToastUtil.showToast(context, result.getMessage());
    }

    @Override
    public void onSMSFailure(Throwable e) {
        HttpUtil.disposeOnFailure(context, e);
    }

    @Override
    public void dismissSMSDialog() {
        dismissProgressDialog();
    }


    @Override
    public void showDialog() {
        showProgressDialog("正在找回密码...");
    }

    @Override
    public void onResult(Result result) {
        AbToastUtil.showToast(context, result.getMessage());
        if (result.getCode() == Config.SUCCESSFUL_CODE) {
          /*  Intent intent = new Intent();
            intent.putExtra("mobile", mEdtMobilePhone.getText().toString().trim());
            intent.putExtra("password", mEdtPassword.getText().toString().trim());
            setResult(RESULT_OK, intent);*/
            finish();
        }
    }

    @Override
    public void onFailure(Throwable e) {
        HttpUtil.disposeOnFailure(context, e);
    }

    @Override
    public void dismissDialog() {
        dismissProgressDialog();
    }


    @OnClick(R.id.tv_return_login)
    public void onClick() {
        finish();
    }
}
